#ifndef H_RCGD_LOG_H
#define H_RCGD_LOG_H

extern unsigned int prevLogTimeOut;


#define LOG_CATA1_NORM			0
#define LOG_CATA1_SPEC			1
#define LOG_CATA1_WARN			2

#define LOG_CATA1_MAX			3

#define LOG_CATA2_NOTHING		0
#define LOG_CATA2_OK			1
#define LOG_CATA2_FAIL			2
#define LOG_CATA2_WARN			3
#define LOG_CATA2_INFO			4
#define LOG_CATA2_IMPORTANT1	5
#define LOG_CATA2_IMPORTANT2	6
#define LOG_CATA2_PLUS_ME		7
#define LOG_CATA2_PLUS_YOU		8
#define LOG_CATA2_MINUS_ME		9
#define LOG_CATA2_MINUS_YOU		10

#define LOG_CATA2_MAX			11


typedef struct LOG_CATA_STR{
	int num;
	char str[16];
}LOG_CATA_STR_;

//hmlee - 100kb for one log file - appx.1260 logs
//hmlee - 50kb for one log file - appx.991 logs
//#define LOG_FILE_MAX_SIZE (1024*50)
#define LOG_FILE_MAX_SIZE (1024*200)
#define GDU_LOG1_PATH	"/gpsclock/GDULog1"
#define GDU_LOG2_PATH	"/gpsclock/GDULog2"
#define GDU_LOG_NEW_PATH	"/gpsclock/log/GRCALogNew"
#define GDU_LOG_OLD_PATH	"/gpsclock/log/GRCALogOld"
//#define GDU_LOG_NEW_PATH	"/tmp/GRCALogNew"
//#define GDU_LOG_OLD_PATH	"/tmp/GRCALogOld"

#define GDU_LOG_FILE_NULL 0
#define GDU_LOG_FILE_RAM 1
#define GDU_LOG_FILE_NEW 2
#define GDU_LOG_FILE_OLD 3

#define ADDRESS_LOG1	0x74000
#define ADDRESS_LOG2	0x7C000

#define LOG_QUEUE_LENGTH	100


/* Diagnosis logging */
#define MAX_LOG				200        
#define MAX_TLOG			200

#define SIZE_OF_1_LOG		14
#define LOG_PER_SECTOR	18


#define LOG_SECTOR_NO		MAX_LOG/LOG_PER_SECTOR+2



#define LOG_Set_To_Default	1

/* Operation.LOCKMODE */
#define LOG_Holdover			2
#define LOG_Lock				3

/* Operation.STATUS */
#define LOG_Active			4
#define LOG_Standby			5
#define LOG_Block				6

#define LOG_1PPS_Alarm		7

#define	LOG_Fault				8

#define LOG_EB_In				11
#define LOG_EB_Out			12
#define LOG_EB_Error			13
#define LOG_OCXO_Error		14

#define LOG_E1_In				15
#define LOG_E1_Out			16

#define LOG_E2_In				19
#define LOG_E2_Out			20


#define LOG_LMU_In			17
#define LOG_LMU_Out			18


#define LOG_SIG_ON			21
#define LOG_SIG_OFF			22


#define LOG_RTC_Error		31
#define LOG_RB_Error			32
#define LOG_RB_LOCK_Error	33
#define LOG_RB_TX_Error	34


#define LOG_PECL				41
#define LOG_10MHz			42
#define LOG_19MHz			43
#define LOG_HDLC_FAIL		44
#define LOG_dDAC_FAIL		45
#define LOG_POWER_FAIL		46
#define LOG_HW_FAIL			47
#define LOG_PP2S_Fault		48
#define LOG_EFC_ERROR		49

#define LOG_EFC_OK			50
#define LOG_dDAC_OK			51

#define	LOG_Receiver_Alarm	61
#define LOG_Receiver_Good	62
#define LOG_TOD_Correction	63

#define LOG_CONTROL_SYNC	71
#define LOG_TRACE_SYNC	72
#define LOG_EVEN_SYNC		73
#define LOG_CONTROL_SYNC_TYPE	74

#define LOG_UPDATE_DAC	81

#define LOG_Antenna_Open	82
#define LOG_Antenna_Short	83

#define LOG_GPS_Lock		84
#define LOG_GPS_Unlock		85

#define LOG_EB_Unlock		86

#define LOG_EB_STATUS		90

#define LOG_DEBUG			99

#define LOG_TRACE			98

#define LOG_Start				100
#define LOG_RESET		    101


#define	LOG_SWITCH_B2AS		103
#define	LOG_SWITCH_PS2S		104
#define	LOG_SWITCH_A2B		105
#define LOG_SWITCH_S2B		106
#define LOG_SWITCH_A2S		107
#define LOG_SWITCH_S2A		108
#define LOG_SWITCH_PS2B		109
#define LOG_DUAL_DATA_NOT_RECEIVE	110

#define LOG_SYNC_ERR		115
#define LOG_RESET_ERR1		116
#define LOG_RESET_ERR2		117

#define LOG_1PPS_LOST		118
#define LOG_1PPS_RESET		119
#define LOG_1PPS_SET		120
#define LOG_1PPS_VAL		121
#define LOG_Antenna_OK		122
#define LOG_EB_Lock			123

#define LOG_RESET_ERR3		124

#define LOG_WDT_OVER		125

#define LOG_SYNC15_FREQ	126
#define LOG_SYNC15_RESET	127
#define LOG_OFF_RPT1		128
#define LOG_1PPS_Ok			129

#define LOG_TFOM				130
#define LOG_COAST			131
#define LOG_RECOVERY		132

#define LOG_POS_HOLD1		133
#define LOG_POS_HOLD2		134

#define LOG_RECOVERY_END 135
#define LOG_39M_CLEAR		136

#define LOG_SYNC15_UPGRADE	137

#define LOG_TOD_CHG_BEFORE	138
#define LOG_TOD_CHG_AFTER	139
#define LOG_MID_CHG_BEFORE	140
#define LOG_MID_CHG_AFTER	141

#define LOG_POWER_OK		142


#define TLog_No_Sat			12
#define TLOG_YEAR_ERR		13

#define TLOG_FUNC_FAIL		15

#define TLOG_1			0
#define TLOG_2			1
#define TLOG_3			2
#define TLOG_4			3
#define TLOG_dDAC_Fail	11
#define TLOG_Fault		0

#define TLOG_TOD_BEFORE		20
#define TLOG_TOD_AFTER		21
#define TLOG_TRAIM_ALM			22
#define TLOG_TRAIM_UNKNOWN	23
#define TLOG_TRAIM_OK			24
#define TLOG_TRAIM_ERROR		25

#define LOG_NORMAL		0
#define LOG_COUNTER		1
#define LOG_PRESET		2
#define	LOG_CONFIRM		3


#define	RESET_MODEL_SYNC15	0
#define	RESET_MODEL_FRUNO		1
#define	RESET_MODEL_BOTH		2

#define	RESET_TYPE_SELFTEST	0
#define	RESET_TYPE_INITSTEP	1
#define	RESET_TYPE_ALMCLEAR	2


typedef struct LOG_DATA_S{
	BYTE	Header[4];
	int		Counter;
    int		ID;

    WORD	YEAR;
    BYTE	MONTH;
    BYTE	DAY;
    BYTE	HOUR;
    BYTE	MINUTE;
    BYTE	SECOND;
    WORD	MSECOND;
    int		PARAMETER1;
    int		PARAMETER2;
    int		PARAMETER3;

}LOG_DATA_;



typedef struct LOG_M{
	int CNT;
	int TimeOut;
    int	LogCounter;
	int	LastLogCounter;
    int	FirstLog;
    int	LastLog;
    int	AccLogCounter;
    int	LogMessageEvent;
    int	LogPresetEvent;
    int	LogMessageNo;
    int	DISPLAY_NO;
    char	Precise_Mode;
    char	Page_Mode;
    char	LogFileType;
}LOG_;


typedef struct TLOG_M{
    WORD	LogCounter;
    WORD	FirstLog;
    WORD	LastLog;
    LONG	AccLogCounter;

    BYTE	DISPLAY_NO;

    LOG_DATA_	Message[MAX_TLOG];
}TLOG_;



extern LOG_	LOG;
extern TLOG_	TLOG;

extern void backup_copy_config(void);
extern void backup_copy_log(void);
extern void Init_LOG();

extern void PresetLOG();

extern char ConfigLogEvent(void);

extern void LogIt(int cata1, int cata2, int name, int parameter);
extern void LogIt3(int cata1, int cata2, int name, int parameter1, int parameter2, int parameter3);
extern void LogItString(int cata1, int cata2, char *s);
extern void LogItString1(int cata1, int cata2, char *s, int v1);
extern void LogItString2(int cata1, int cata2, char *s, int v1, int v2);
extern void LogItString3(int cata1, int cata2, char *s, int v1, int v2, int v3);
extern void LogItString6(int cata1, int cata2, char *s, unsigned short v1, 
	unsigned char v2, unsigned char v3, unsigned char v4, unsigned char v5, unsigned char v6);


extern char LogStartFlag;
extern char Setting_LOG_Flag;

extern int found_log, first_log, last_log, acc_log;

#define FLASH_ERASE_FAIL_EX			0
#define FLASH_TOGGLE_PENDING_EX		1
#define FLASH_SUCCESS_EX			2
#define FLASH_TOGGLE_FAIL_EX		3
#define FLASH_READY_EX				4
#define FLASH_TOGGLE_SUCCESS_EX		5



#define ConvertEBss3(origin,x,y,z)         \
      (                                     \
            (origin)|                        \
            ( ((x)<<24)&0xFF000000 )|   \
            ( ((y)<<16)&0x00FF0000 )|   \
            ( ((z)<< 8)&0x0000FF00 )    \
      )
      
#define 	LogEbStatus();			LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_EB_STATUS,\
    				ConvertEBss3( (int)SYSTEM.TRACK.VISIBLE, SYSTEM.TRACK.SAT[0].SS, SYSTEM.TRACK.SAT[1].SS, SYSTEM.TRACK.SAT[2].SS), \
    				ConvertEBss3( (int)SYSTEM.TRACK.TRACKED, SYSTEM.TRACK.SAT[3].SS, SYSTEM.TRACK.SAT[4].SS, SYSTEM.TRACK.SAT[5].SS), \
    				ConvertEBss3( (int)SYSTEM.TRACK.DOP    , SYSTEM.TRACK.SAT[6].SS, SYSTEM.TRACK.SAT[7].SS,( (  (int)(STATUS.T_EXT/ 10.0) )&0xFF) ) \
				);

#define 	log_ddac_fail();		LogIt3(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_dDAC_FAIL,\
    				(int)TEMP_RAW_DIGITS, \
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)MeanDAC & 0xFFFF), \
    				(((int)(CONTROL.AvgTimeOffset*TENMILLION))<<16 & 0xFFFF0000) | ((int)(CONTROL.TimeOffset*TENMILLION) & 0xFFFF) \
				);
				
#define 	log_ddac_ok();			LogIt3(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_dDAC_OK,\
    				(int)TEMP_RAW_DIGITS, \
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)MeanDAC & 0xFFFF), \
    				(((int)(CONTROL.AvgTimeOffset*TENMILLION))<<16 & 0xFFFF0000) | ((int)(CONTROL.TimeOffset*TENMILLION) & 0xFFFF) \
				);
				 
#define 	log_1pps_alarm_on();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_1PPS_Alarm,\
    				(int)TEMP_RAW_DIGITS, \
    				(int)(CONTROL.AvgTimeOffset*TENMILLION), \
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | (SYSTEM.TRACK.TRACKED<<8 & 0xFF00) | \
    				(control_mode & 0xFF) \
				);
						
#define 	log_1pps_alarm_off();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_1PPS_Ok,\
    				(int)TEMP_RAW_DIGITS, \
    				(int)(CONTROL.AvgTimeOffset*TENMILLION), \
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | (SYSTEM.TRACK.TRACKED<<8 & 0xFF00) | \
    				(control_mode & 0xFF) \
				);		
				
#define 	log_efc_err();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_FAIL, LOG_EFC_ERROR,\
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)OCXO_ID.TDAC & 0xFFFF), \
    				(int)(CONTROL.AvgTimeOffset*TENMILLION), \
    				(((int)(CONTROL.TimeOffset*TENMILLION))<<16 & 0xFFFF0000) | (SYSTEM.TRACK.TRACKED<<8 & 0xFF00) | \
    				((int)(STATUS.T_EXT/ 10.0) & 0xFF) \
				);
						
#define 	log_efc_ok();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_EFC_OK,\
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)OCXO_ID.TDAC & 0xFFFF), \
    				(int)(CONTROL.AvgTimeOffset*TENMILLION), \
    				(((int)(CONTROL.TimeOffset*TENMILLION))<<16 & 0xFFFF0000) | (SYSTEM.TRACK.TRACKED<<8 & 0xFF00) | \
    				((int)(STATUS.T_EXT/ 10.0) & 0xFF) \
				);		


#define 	log_offset_report1();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_OFF_RPT1,\
					(int)TEMP_RAW_DIGITS, \
    				(int)(last_ro_value), \
    				(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | (SYSTEM.TRACK.TRACKED<<8 & 0xFF00) | \
    				((int)(STATUS.T_EXT/ 10.0) & 0xFF) \
				);			
				
#define 	log_offset_report2();	LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_OFF_RPT2,\
					(int)tr1.unlock_counter, \
    				(int)tr1.mode_1_error_counter, \
    				(int)tr1.parity_1_error_counter \
				);			

#define 	log_1pps_offline();		LogIt3(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_Receiver_Alarm,\
    				(((int)glov_last<<16)&0xFFFF0000) | (glov_off&0xFFFF), \
					(((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)OCXO_ID.TDAC & 0xFFFF), \
					(int)tr1.unlock_counter \
				);
	
#define 	log_1pps_online();		LogIt3(LOG_CATA1_NORM, LOG_CATA2_OK, LOG_Receiver_Good,\
					((((int)online_alarm_range)<<16 & 0xFFFF0000) | (glov_off&0xFFFF) ),\
					((((int)CONTROL.RealDAC)<<16 & 0xFFFF0000) | ((int)OCXO_ID.TDAC & 0xFFFF) ),\
					(int)tr1.unlock_counter \
				);
				
#define		log_pos_hold1();			LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_POS_HOLD1,\
					(((short)(SYSTEM.POSITION.AVERAGE.LAT/3600000))<<16 &0xFFFF0000) | \
					((short)((SYSTEM.POSITION.AVERAGE.LAT%3600000)/60000)&0x0000FFFF), \
					(((short)(SYSTEM.POSITION.AVERAGE.LON/3600000))<<16 &0xFFFF0000) | \
					((short)((SYSTEM.POSITION.AVERAGE.LON%3600000)/60000)&0x0000FFFF), \
					(int)(SYSTEM.POSITION.AVERAGE.HEI) \
				);
				
#define		log_pos_hold2();			LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_POS_HOLD2,\
					(int)((SYSTEM.POSITION.AVERAGE.LAT%3600000)%60000), \
					(int)((SYSTEM.POSITION.AVERAGE.LON%3600000)%60000), \
					0 \
				);

			
extern int child_calling_exit;

void PresetTLOG();
void TLogIt(int name, int parameter);

#endif


